document.getElementById('pwdForm').addEventListener('submit', async function(e) {
    e.preventDefault();
    
    const userInfo = JSON.parse(localStorage.getItem('userInfo'));
    if (!userInfo || !userInfo.id) {
        alert('请先登录！');
        location.href = './login.html';
        return;
    }

    const oldPassword = document.getElementById('oldPassword').value;
    const newPassword = document.getElementById('newPassword').value;
    const rNewPassword = document.getElementById('rNewPassword').value;

    if (!oldPassword || !newPassword || !rNewPassword) {
        alert('所有密码字段都必须填写！');
        return;
    }

    if (newPassword !== rNewPassword) {
        alert('两次输入的新密码不一致！');
        return;
    }

    const data = new URLSearchParams();
    data.append('id', userInfo.id);
    data.append('oldPassword', oldPassword);
    data.append('newPassword', newPassword);
    data.append('rNewPassword', rNewPassword);

    try {
        const res = await axios.post('/users/rpwd', data);
        if (res.data.code === 1) {
            alert('修改密码成功，请重新登录！');
            localStorage.removeItem('token');
            localStorage.removeItem('userInfo');
            location.href = './login.html';
        } else {
            alert('修改密码失败：' + res.data.message);
        }
    } catch (err) {
        console.error('修改密码失败:', err);
        alert('修改密码失败，请重试');
    }
}); 